package com.ruoyi.common.utils;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.project.business.domain.model.daily.TimePeriodMirror;
import com.ruoyi.project.business.domain.model.month.ShiftDefine;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

/**
 * 时间工具类
 *
 * @author thanks
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        return dateTimeNow(format, date);
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate(Date date) {
        return dateTimeNow(YYYY_MM_DD, date);
    }

    public static final String getTime(Date date) {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS, date);
    }

    public static final String dateTimeNow(Date date) {
        return dateTimeNow(YYYYMMDDHHMMSS, date);
    }

    public static final String dateTimeNow(final String format, Date date) {
        return parseDateToStr(format, date);
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 获取指定时间所在月第一天的0时0分0秒
     *
     * @param date
     * @return
     */
    public static Date getMonthStartTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        Date result = null;
        try {
            result = sdf.parse(
                    org.apache.commons.lang.time.DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 00:00:00");
        } catch (ParseException e) {

        }
        return result;
    }

    /**
     * 获取指定时间所在月最后一天的23:59:59
     *
     * @param date
     * @return
     */
    public static Date getMonthEndTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //设置为月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date result = null;
        try {
            result = sdf.parse(
                    org.apache.commons.lang.time.DateFormatUtils.format(c.getTime(), "yyyy-MM-dd") + " 23:59:59");
        } catch (ParseException e) {

        }
        return result;
    }


    /**
     * 获取date后的x月的时间，如果x为负数，则是之前x月
     *
     * @param date
     * @param n    月数
     * @return
     */
    public static Date getAfterMonthDate(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);
        return calendar.getTime();
    }

    /**
     * 获取date日期当天的开始时间
     */
    public static Date getStartTimeOfDay(Date date) {
        if (null == date) {
            date = new Date();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取date日期当天的结束时间
     */
    public static Date getEndTimeOfDay(Date date) {
        if (null == date) {
            date = new Date();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 计算date后的days天的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date calNewDate(Date date, int days) {
        if (null == date) {
            return date;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }

    /**
     * date增加指定的分钟数。
     *
     * @param date
     * @param minutes 加的分钟数量。正数为加，负数为减
     * @return
     */
    public static Date getMinuteAddTime(Date date, int minutes) {
        if (null == date) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

    /**
     * 加上指定长度的月份后的时间，负数为减
     *
     * @param date
     * @param monthSize
     * @return
     */
    public static Date addMonth(Date date, Integer monthSize) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, monthSize);
        return cl.getTime();
    }

    /**
     * 返回时间
     * @param year
     * @param month
     * @return
     */
    public static List<LocalDate> getPublicHolidays(int year, int month) {
        List<LocalDate> publicHolidays = new ArrayList<>();
        YearMonth yearMonth = YearMonth.of(year, month);
        int daysInMonth = yearMonth.lengthOfMonth();

        for (int day = 1; day <= daysInMonth; day++) {
            LocalDate date = LocalDate.of(year, month, day);
            DayOfWeek dayOfWeek = date.getDayOfWeek();
            if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
                publicHolidays.add(date);
            }
        }
        return publicHolidays;
    }

    /**
     * 返回 日历
     * @param year
     * @param month
     * @return
     */
    public static List<Integer> getPublicHolidayIndexes(int year, int month) {
        List<Integer> publicHolidays = new ArrayList<>();
        YearMonth yearMonth = YearMonth.of(year, month);
        int daysInMonth = yearMonth.lengthOfMonth();

        for (int day = 1; day <= daysInMonth; day++) {
            LocalDate date = LocalDate.of(year, month, day);
            DayOfWeek dayOfWeek = date.getDayOfWeek();
            if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
                publicHolidays.add(day);
            }
        }
        return publicHolidays;
    }


    public static boolean ifMonday(LocalDate aimDay) {
        DayOfWeek dayOfWeek = aimDay.getDayOfWeek();
        return DayOfWeek.MONDAY.equals(dayOfWeek);
    }

    public static boolean ifSunday(LocalDate aimDay) {
        DayOfWeek dayOfWeek = aimDay.getDayOfWeek();
        return DayOfWeek.SUNDAY.equals(dayOfWeek);
    }

    public static boolean ifSaturday(LocalDate aimDay) {
        DayOfWeek dayOfWeek = aimDay.getDayOfWeek();
        return DayOfWeek.SATURDAY.equals(dayOfWeek);
    }

    public static boolean ifDay(LocalDate aimDay, DayOfWeek dayOfWeek) {
        return dayOfWeek.equals(aimDay.getDayOfWeek());
    }

    public static List<LocalDate> listDates(YearMonth yearMonth) {
        List<LocalDate> dateList = new ArrayList<>();
        int daysInMonth = yearMonth.lengthOfMonth(); // 获取月份的天数
        for (int i = 1; i <= daysInMonth; i++) {
            dateList.add(yearMonth.atDay(i)); // 将日期添加到列表中
        }
        return dateList;
    }

    public static List<ShiftDefine> joinDateAndScheduleDefine(int year, int month) {
        List<LocalDate> aimDays = listDates(YearMonth.of(year, month));
        List<ShiftDefine> mixed = new ArrayList<>();
        for (LocalDate date : aimDays) {
            ShiftDefine define = new ShiftDefine();
            define.setAimDate(date);
            mixed.add(define);
        }
        return mixed;
    }

    public static Date changeDate(Date origin, LocalDate aim) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(origin); // 设置当前时间
        calendar.set(Calendar.YEAR, aim.getYear()); // 更改年份
        calendar.set(Calendar.MONTH, aim.getMonthValue()); // 更改月份（注意月份从0开始，即0对应1月）
        calendar.set(Calendar.DATE, aim.getDayOfMonth()); // 更改日期
        return calendar.getTime();
    }

    public static Date transferTime(LocalDate localDate, LocalTime time) {
        if(time == null) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atTime(time).atZone(ZoneId.systemDefault());

        // 转换为java.util.Date
        Date date = Date.from(zonedDateTime.toInstant());
        return date;
    }

    public static LocalTime toLocalTime(Date date) {
        // 将java.util.Date转换为Instant
        Instant instant = date.toInstant();
        // 将Instant转换为ZonedDateTime，使用系统默认时区
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        // 从ZonedDateTime提取LocalTime
        LocalTime localTime = zonedDateTime.toLocalTime();
        return localTime;
    }



    public static Integer month(Date yearMonth) {
        if(yearMonth == null) {
            return null;
        }
        return DateUtil.month(yearMonth) + 1;
    }

    public static Integer year(Date yearMonth) {
        if(yearMonth == null) {
            return null;
        }
        return DateUtil.year(yearMonth);
    }

    public static int getHours(LocalDateTime applyTimeStart, LocalDateTime applyTimeEnd) {
        Duration duration = Duration.between(applyTimeStart, applyTimeEnd);

        // 输出时间差的小时、分钟和秒数
        // TODO 时间应该还要考虑休息时间、半天、整天等
        Long hours = duration.toHours();
        if(hours > 8) {
            return 8;
        } else if (hours < 4) {
            return 4;
        } else if (hours >= 4 && hours <= 8) {
            return 8;
        } else {
            return 0;
        }
//        long minutes = duration.toMinutes() % 60;
//        long seconds = duration.getSeconds() % 60;
//        return hours.intValue();
    }

    public static List<TimePeriodMirror> splitTo30Minute(LocalTime assignTimeStart, LocalTime assignTimeEnd) {
        List<TimePeriodMirror> tmpList = new ArrayList<>();
        LocalTime start = assignTimeStart;
        LocalTime end = null;
        do {
            end = start.plusMinutes(30);
            TimePeriodMirror timePeriodMirror = new TimePeriodMirror();
            timePeriodMirror.setSegmentStart(start);
            timePeriodMirror.setSegmentEnd(end);
            tmpList.add(timePeriodMirror);
            start = end;
        }while(start.isBefore(assignTimeEnd));
        return tmpList;
    }

    public static LocalDate yestoday() {
        return LocalDate.now().minusDays(1);
    }
}
